package com.order.controller;


import com.common.common.BaseResponse;
import com.common.common.ResultUtils;
import com.common.controller.AbstractTypedController;
import com.order.entities.Orders;
import com.order.feign.StoreFeignService;
import com.order.service.OrderServiceImpl;
import com.order.vo.OrderVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Component
@RestController
@RequestMapping("orders")
@Slf4j
@CrossOrigin
@Tag(name = "订单")
public class OrdersController extends AbstractTypedController<Orders, Integer> {
    @Resource
    OrderServiceImpl odvc;

    @Resource
    StoreFeignService storeFeignService;

    OrdersController(OrderServiceImpl orderService ) {
        this.odvc = orderService;
        this.svcContext = orderService;
    }

    @PostMapping("/order")
    @Operation(summary = "创建订单")
    public BaseResponse<Orders> order( @RequestBody OrderVo orderVo) {
        return ResultUtils.success(odvc.order(orderVo.getUid(), Integer.valueOf(orderVo.getBid()), Double.valueOf(orderVo.getTotal())));
    }


    @GetMapping("/getorderbybid/{bid}")
    @Operation(summary = "通过bid查找订单")
    public BaseResponse<List<Orders>> GetOrderBybid(@PathVariable Integer bid) {
        return ResultUtils.success(odvc.findBybid(bid));
    }

    @GetMapping("/testFeignTriggerSentinel")
    @Operation(summary = "Feign触发Sentinel")
    public Integer FeignTriggerSentinel() {
        return storeFeignService.FeignTriggerSentinel();
    }


    @GetMapping("/getorderbyuid/{uid}")
    @Operation(summary = "通过uid查找订单")
    public BaseResponse<List<Orders>> GetOrderByuid(@PathVariable Integer uid) {
        return ResultUtils.success(odvc.findByuid(uid));
    }


    @PostMapping("/payorder")
    @Operation(summary = "支付订单")
    public BaseResponse<Boolean> payOrder(@RequestParam Integer orderId){
        return ResultUtils.success(odvc.pay(orderId));
    }
}
